var axiomListActions = null;

function AxiomListingActions(parent) {
	var axiomListingDialog = null;
	var containerManager = null;
	var conditionManager = null;
	
	function loadDialog() {
		if(axiomListingDialog === null) {
			axiomListingDialog = $('.axiomListingDialog');
			axiomListingDialog.find('#type').selectmenu({
				width: '100%',
				open: function() {
					$('.ui-selectmenu-menu-popup').css('z-index', 9999);
				},
				change: onTypeChange
			});
			
			var buttons = axiomListingDialog.find('.button').button();
			buttons.filter('.ok').button('option', 'icons', {primary:'ui-icon-plus' });
			buttons.filter('.useCondition').button('option', 'icons', {primary:'ui-icon-check', secondary:'ui-icon-triangle-1-s'})
				.click(function() {
					if($(this).attr('checked')) {
						axiomListingDialog.find('.conditionProperties').show('blind');
					} else {
						axiomListingDialog.find('.conditionProperties').hide('blind');
					}
				});
			
			axiomListingDialog.dialog({
				modal: true,
				autoOpen: false,
				width: 400,
				hide: 'blind',
				show: 'blind'
			});	
			
			containerManager = new ContainerManager(axiomListingDialog);
			conditionManager = new ConditionManager(axiomListingDialog);
		}
		var axiomProperties = axiomListingDialog.find('.axiomProperties').children();
		axiomProperties.hide();
		axiomListingDialog.find('label[for="useCondition"]').hide();
		var type = axiomListingDialog.find('#type');
		type.selectmenu('value', '');
		onTypeChange(type);
	}
	
	function onTypeChange(select, param) {
		if(select.currentTarget)
			var val = $(this).val();
		else 
			var val = select.val();
		
		var axiomProperties = axiomListingDialog.find('.axiomProperties').children();
		var useCondition = axiomListingDialog.find('label[for="useCondition"]');
		var conditionProperties = axiomListingDialog.find('.conditionProperties');
		
		axiomProperties.filter(':visible').hide('blind');
		
		if(val.length > 0) {
			var axiomProperty = axiomProperties.filter('.' + val + 'AxiomProperties:hidden');

			if(useCondition.is(':hidden')) {
				useCondition.show('blind');
			}
			if(useCondition.hasClass('ui-state-active') && conditionProperties.is(':hidden')) {
				conditionProperties.show('blind');
			}
			axiomProperty.show('blind');
		} else {
			useCondition.hide('blind');
			
			if(conditionProperties.is(':visible')) {
				conditionProperties.hide('blind');
			} else {
				conditionProperties.hide();
			}
		}	
	}
	
	function newAxiom() {
		loadDialog();
		
		axiomListingDialog.find('.ok').unbind('click').click(function() {
			if($.trim(axiomListingDialog.find('.type').val()).length === 0) {
				messageBox.showMessage('Please select type from list.');
			} else {
				axiomListingDialog.dialog('close');
			}
		});
		axiomListingDialog.dialog('open');		
	}
	
	this.load = function (header, content, data) {
		content.prepend(data);
//		if(!data.length) {
//			content.append("<p>There are no installation codes.</p>");
//		} else {		
//			var authSelectAccess = function() {
//				return '<select class="ui-state-default"><option value="CLIENT" >CLIENT</option><option value="MANAGER" >MANAGER</option><option value="ADMIN" >ADMIN</option></select>';
//			}
//			var actionsAccess = function () {
//				return '<span class="button changePassword">Change password</span><span class="button remove">Remove</span></td>';
//			};
//			var tableParams = {
//				columnNames: ['Username', 'Password', 'Authorization', 'Actions'],
//				columnProperties: [{access:  'username'}, {access: 'password'}, {access: authSelectAccess, className: 'authColumn'}, {access: actionsAccess}]
//			};
//			var table = util.buildSnazzyTable(tableParams, data);
//			content.append(table);	
//			
//			for(var userIndex in data) {
//				var authSelect = content.find('tbody > tr:eq(' + userIndex + ') .authColumn select');
//				authSelect.val(data[userIndex].authorizationLevel);
//				
//				authSelect.selectmenu();
//			}
//		}
//		content.prepend('<div class="actions"><span class="button newUser">New user</span></div>');
		
//		var buttons = content.find('.button').button();
//		buttons.filter('.newAxiom')
//			.button('option', 'icons', {primary:'ui-icon-plus' })
//			.click(modifyPassword);	
		
	};
	
	function init() {
		var buttons = parent.find('.button').button();
		buttons.filter('.newAxiom')
			.button('option', 'icons', {primary:'ui-icon-plus' })
			.click(newAxiom);		
	}
	
	init();
}

function ContainerManager(parent) {
	function init() {
		parent.find('#containerType').selectmenu({
			width: 150,
			open: function() {
				$('.ui-selectmenu-menu-popup').css('z-index', 9999);
			}						
		});		
	}
	
	init();
}

function ConditionManager(parent) {
	function init() {
		parent.find('#conditionType').selectmenu({
			width: '100%',
			open: function() {
				$('.ui-selectmenu-menu-popup').css('z-index', 9999);
			}				
		});
	}
	
	init();
}
